package org.andao.commons.lucene.core;

import org.andao.core.config.ConfigPropertyReader;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.BinaryRequestWriter;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.springframework.stereotype.Component;


/**
 * solr server context. solrserver上下文，对luceneservercontext
 * <p>
 * solrj <--> solr server.
 * 
 * @author Seewo Software - Marco.hu(huzhiguo@cvte.cn)
 * 
 */
@SuppressWarnings("deprecation")
@Component("solrServerContext")
public class SolrServerContext implements LuceneServerContext {

	private static CommonsHttpSolrServer server = null;

	public SolrServerContext() {
		super();
	}

	// TODO @vick Props below should be saved in solr.properties(Creat it!)
	public SolrServer getServer() {
		try {
			String url = ConfigPropertyReader.getInstance().getProperty("solr.server.url");		
			if(null== url){
				url = "http://172.18.48.83:8088/solr/core_iclass/";
			}
			server = new CommonsHttpSolrServer(url);
			server.setSoTimeout(30000);
			server.setConnectionTimeout(30000);
			server.setDefaultMaxConnectionsPerHost(100);
			server.setMaxTotalConnections(100);
			server.setAllowCompression(true);
			server.setMaxRetries(1);
			server.setRequestWriter(new BinaryRequestWriter());// 使用二进制流输出
		} catch (Exception e) {
			e.printStackTrace();
		}

		return server;
	}
}
